package com.bizfx.component.fields
{
	import flash.events.Event;
	
	import mx.controls.ButtonLabelPlacement;
	import mx.controls.CheckBox;
	import mx.core.IFlexDisplayObject;
	import mx.core.mx_internal;
	import mx.events.FlexEvent;
		
	use namespace mx_internal;
	
	public class CheckBoxItem extends CheckBox
	{
		public function CheckBoxItem()
		{
			super();
			this.setStyle("disabledOverlayAlpha", 0);
			this.setStyle("disabledColor", 0x000000);
			addEventListener(FlexEvent.ADD, addHandler);
		}
		
		[Bindable("labelPlacementChanged")]
		[Inspectable(category="General", enumeration="left,right,top,bottom", defaultValue="right")]
		override public function get labelPlacement():String
		{
			var value:String = ButtonLabelPlacement.RIGHT;
			
			if (_labelPlacement != "")
				value = _labelPlacement;
			else if (_group && _group.labelPlacement != "")
				value = _group.labelPlacement;
			
			return value;
		}

		private var _group:CheckBoxGroupField;
		
		public function get group():CheckBoxGroupField
		{
			// Debugger asks too soon.
			if (!document)
				return _group;
			
			if (!_group)
			{
				if (groupName && groupName != "")
				{
					try 
					{
						_group = document[groupName] as CheckBoxGroupField;
					}
					catch(err:Error)
					{
					}
				}
			}
			
			return _group;
		}
		
		public function set group(value:CheckBoxGroupField):void
		{
			_group = value;
		}

		private var _value:String;
		
		[Inspectable(category="General", defaultValue="")]
		public function get value():String
		{
			return _value;
		}
		
		public function set value(value:String):void {
			_value = value;
		}
		
		mx_internal var _groupName:String;
		
		private var groupChanged:Boolean = false;
		
		[Bindable("groupNameChanged")]
		[Inspectable(category="General", defaultValue="radioGroup")]
		public function get groupName():String
		{
			return _groupName;
		}
		
		public function set groupName(value:String):void
		{
			if (!value || value == "")
				return;
			
			if (value == this._groupName) {
				return;
			}
			deleteGroup(); // Delete the old group
			
			_groupName = value;

			groupChanged = true;

			invalidateProperties();
			invalidateDisplayList();
			
			dispatchEvent(new Event("groupNameChanged"));
		}

		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if (groupChanged)
			{
				addToGroup();
				
				groupChanged = false;
			}
		}
		
		private function addHandler(event:FlexEvent):void
		{
			if (!_group && initialized)
				addToGroup();
		}
		
		private function addToGroup():Object
		{
			var g:CheckBoxGroupField = group; // Trigger getting the group
			if (g)
				g.addInstance(this);
			return g;
		}

		mx_internal function deleteGroup():void
		{
			try
			{
				if (document[groupName])
					delete document[groupName];
			}
			catch(e:Error)
			{
				try
				{
					if (document.automaticCheckBoxGroupFields[groupName])
						delete document.automaticCheckBoxGroupFields[groupName];
				}
				catch(e1:Error)
				{
				}
			}
		}
	}
}